package com.example.zkweixiu.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.zkweixiu.app.MainActivity;
import com.example.zkweixiu.app.R;
import com.example.zkweixiu.app.activity.AddUserDataActivity;
import com.example.zkweixiu.app.activity.LoginActivity;
import com.example.zkweixiu.app.activity.TaskActivity;
import com.example.zkweixiu.app.activity.UserManageActivity;
import com.example.zkweixiu.app.myview.RxRunTextView;
import com.example.zkweixiu.app.pro.PublicPro;
import com.littlegreens.netty.client.NettyTcpClient;
import com.littlegreens.netty.client.listener.NettyClientListener;
import com.vondear.rxtool.RxActivityTool;

import org.joda.time.DateTime;
import org.joda.time.Days;

import me.zhanghai.android.materialratingbar.MaterialRatingBar;

public class HomeFragment1 extends Fragment implements View.OnClickListener {


    private RelativeLayout rl_int1;
    private RelativeLayout rl_int2;
    private Intent intent;
    private TextView tv_tiezhu;

    private NettyTcpClient mNettyTcpClient;
    private static String ponse;
    private RxRunTextView rx_text_run;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, container, false);

        return view;

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        rl_int1 = view.findViewById(R.id.rl_int1);
        rl_int2 = view.findViewById(R.id.rl_int2);

        tv_tiezhu = view.findViewById(R.id.tv_tiezhu);   // 昵称

        TextView tv_tiezhu2 = view.findViewById(R.id.tv_tiezhu2);   // 在岗天数
        View view_add_user = view.findViewById(R.id.view_add_user);   // 在岗天数
        // 滚动
        rx_text_run = view.findViewById(R.id.rx_text_run);
        MaterialRatingBar library_normal_ratingbar = view.findViewById(R.id.library_normal_ratingbar);   // 星级
        //library_normal_ratingbar.setEnabled(false);
        //library_normal_ratingbar.setWillNotDraw(false);
        if (PublicPro.userBean != null) {
            tv_tiezhu.setText(PublicPro.userBean.getData().getGuaName());

            library_normal_ratingbar.setRating(PublicPro.userBean.getData().getGuaLevel());

            DateTime dt1 = new DateTime(PublicPro.userBean.getData().getGuaTime()); //账号创建时间
            DateTime dt2 = new DateTime(new DateTime().getMillis()); //当前时间
            tv_tiezhu2.setText("在岗"+Days.daysBetween(dt1, dt2).getDays() + "天 "); //计算时间差
        }else {
            RxActivityTool.skipActivityAndFinishAll(getActivity(), LoginActivity.class);
        }


        rl_int1.setOnClickListener(this);
        view_add_user.setOnClickListener(this);
        rl_int2.setOnClickListener(this);
/*
        rl_int1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivity.class);

                startActivity(intent);
            }
        });*/
        sendService();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.view_add_user:
                intent = new Intent(getActivity(), AddUserDataActivity.class);

                startActivity(intent);
                break;

            case R.id.rl_int1:
                intent = new Intent(getActivity(), TaskActivity.class);

                startActivity(intent);
                break;

            case R.id.rl_int2:
                intent = new Intent(getActivity(), UserManageActivity.class);

                startActivity(intent);
                break;

        }
    }

    private void sendService() {
        mNettyTcpClient = new NettyTcpClient.Builder()
                .setHost(PublicPro.SERVICE_TCP)    //设置服务端地址
                .setTcpPort(8192) //设置服务端端口号
                .setMaxReconnectTimes(5)    //设置最大重连次数
                .setReconnectIntervalTime(5)    //设置重连间隔时间。单位：秒
                .setSendheartBeat(true) //设置发送心跳
                .setHeartBeatInterval(19)    //设置心跳间隔时间。单位：秒
                // .setHeartBeatData(new byte[]{0x03, 0x0F, (byte) 0xFE, 0x05, 0x04, 0x0a}) //设置心跳数据，可以是String类型，也可以是byte[]
                .setHeartBeatData("00@#&_&%&") //设置心跳数据，可以是String类型，也可以是byte[]，以后设置的为准
                .setIndex(100)    //设置客户端标识.(因为可能存在多个tcp连接)
                .build();

        mNettyTcpClient.setListener(new NettyClientListener() {
            @Override
            public void onMessageResponseClient(Object msg, int index) {
                ponse = msg.toString();
                Log.e("sendUnitKG", ponse);

                if (ponse.contains(PublicPro.SYSTEM_IM)){
                    String[] split = ponse.split("@_@%#",2);
                    rx_text_run.setText("系统消息:".concat(split[1]).concat("                                                                                "));
                }

            }

            @Override
            public void onClientStatusConnectChanged(int statusCode, int index) {
                //  Log.e("setListener", String.valueOf(statusCode));
            }
        });


        mNettyTcpClient.connect();
    }


/*    @Override
    public void onStart() {
        super.onStart();
        if (!mNettyTcpClient.getConnectStatus()) {//没有连接 连接
            mNettyTcpClient.connect();

        }
    }*/
}
